%{
#include <stdio.h>
#include "../include/define.h"
#include "../include/parseMagic.h"

int magicsQuantity = 0;
int basicsQuantity = 0;

static int genIndex = 0;

%}


NUMBER [0-9]

SPACE [ ]

DOTS [:]

COMMA [,]

ENDOFMAGIC [\n]

BASIC 	{SPACE}*{NUMBER}+{DOTS}{SPACE}*{ENDOFMAGIC}

MAGIC	{SPACE}*{NUMBER}+{DOTS}{SPACE}+{NUMBER}+{SPACE}{NUMBER}+

MOREMAGIC	{COMMA}{SPACE}*{NUMBER}+{SPACE}{NUMBER}+{SPACE}*

%%

{MAGIC} {

	int gen, first, second, i;
	
	sscanf(yytext, "%d: %d %d", &gen, &first, &second);
	
	genIndex = gen;

	if(elements[first - 1].name!=NULL && elements[second - 1].name!=NULL && elements[genIndex - 1].name!=NULL){			
		magics = realloc(magics, (magicsQuantity + 2) * sizeof(Magic));
		
		magics[magicsQuantity].e1 = malloc(sizeof(Element));
		magics[magicsQuantity].e1->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].e1->name, elements[first - 1].name);
		
		
		magics[magicsQuantity].e2 = malloc(sizeof(Element));
		magics[magicsQuantity].e2->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].e2->name, elements[second - 1].name);
		
		magics[magicsQuantity].generated = malloc(sizeof(Element));
		magics[magicsQuantity].generated->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].generated->name, elements[genIndex - 1].name);
		
		magics[magicsQuantity+1].generated = NULL;

		magicsQuantity ++;
	}else{
		printf("ERROR: Hay errores en el archivo .magic\n");
		printf("Indice incorrecto\n");
		exit(1);
	}
	
}

{MOREMAGIC} {

	int i;

	
	magics = realloc(magics, (magicsQuantity + 2) * sizeof(Magic));
	
	int gen, first, second;
	
	sscanf(yytext, ", %d %d", &first, &second);
		
	if(elements[first - 1].name!=NULL && elements[second - 1].name!=NULL && elements[genIndex - 1].name!=NULL){
		magics[magicsQuantity].e1 = malloc(sizeof(Element));
		magics[magicsQuantity].e1->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].e1->name, elements[first - 1].name);
		
		
		magics[magicsQuantity].e2 = malloc(sizeof(Element));
		magics[magicsQuantity].e2->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].e2->name, elements[second - 1].name);
		
		magics[magicsQuantity].generated = malloc(sizeof(Element));
		magics[magicsQuantity].generated->name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].generated->name, elements[genIndex - 1].name);
		
		magics[magicsQuantity+1].generated = NULL;
		
		magicsQuantity ++;
	}else{
		printf("ERROR: Hay errores en el archivo .magic\n");
		printf("Indice incorrecto\n");
		exit(1);
	}
}

{BASIC} {	
	int first, i;
	
	for(i = 0; basics != NULL && basics[i].name != NULL ; i++);
	
	sscanf(yytext, "%d:", &first);

	if(elements[first - 1].name!=NULL){
		basics = realloc(basics, (basicsQuantity + 2) * sizeof(Element));
		basics[basicsQuantity].name = malloc((sizeof(elements[first - 1].name) + 1) * sizeof(char *));
		strcpy(basics[basicsQuantity].name, elements[first-1].name);
		basics[basicsQuantity+1].name = NULL;
		basicsQuantity ++;

		magics = realloc(magics, (magicsQuantity + 2) * sizeof(Magic));
		
		int gen;
		
		sscanf(yytext, "%d:", &gen);
			
		
		magics[magicsQuantity].e1 = malloc(sizeof(Element));
		magics[magicsQuantity].e1->name = malloc((sizeof(elements[gen - 1].name) + 1) * sizeof(char *));
		magics[magicsQuantity].e1->name="-1";
		
		
		magics[magicsQuantity].e2 = malloc(sizeof(Element));
		magics[magicsQuantity].e2->name = malloc((sizeof(elements[gen - 1].name) + 1) * sizeof(char *));
		magics[magicsQuantity].e2->name="-1";
		
		magics[magicsQuantity].generated = malloc(sizeof(Element));
		magics[magicsQuantity].generated->name = malloc((sizeof(elements[gen - 1].name) + 1) * sizeof(char *));
		strcpy(magics[magicsQuantity].generated->name, elements[gen - 1].name);
		
		magics[magicsQuantity+1].generated = NULL;
		
		magicsQuantity ++;
	}else{
		printf("ERROR: Hay errores en el archivo .magic\n");
		printf("Indice incorrecto\n");
		exit(1);
	}

}

SPACE  {}

DOTS  {}

COMMA {}

ENDOFMAGIC {}

\n {}

. {
	if(yytext[0] != ' ') {
		printf("ERROR: Hay errores en el archivo .magic\n");
		printf("Este archivo solo puede contener numeros, espacios, enters, dos puntos o comas\n");
		exit(1);
	}
}

%%

int yywrap() {
	return(1);
}

Magic* getMagics(char * magicFile, int* cant)
{	magics=NULL;
	basics=NULL;
	yyin = fopen(magicFile, "r");
	if(yyin == NULL) {
		printf("ERROR: El archivo .magic especficado no existe o no se puede abrir\n");
		exit(1);
	}
	yylex();
	fclose(yyin);
	*cant=magicsQuantity;
	return magics;
}
